/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	MainFrameServiceImpl.java
 * 模块说明：	
 * 修改历史：
 * Dec 19, 2007 - lxm - 创建。
 */
package com.hd123.rumba.gwt.entry.server;

import java.util.ArrayList;
import java.util.List;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.cartridge.ICartFunctionView;
import com.hd123.rumba.cartridge.biz.BCartFunctionView;
import com.hd123.rumba.cartridge.biz.IBCartridgeMgr;
import com.hd123.rumba.domain.IDomainMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.gwt.base.client.exception.ClientBizException;
import com.hd123.rumba.gwt.base.server.RBRemoteServiceServlet;
import com.hd123.rumba.gwt.entry.client.MainFrameService;
import com.hd123.rumba.gwt.entry.client.menu.QMenu;
import com.hd123.rumba.gwt.entry.client.menu.QMenuItem;
import com.hd123.rumba.gwt.entry.server.menu.QMenuItemReader;
import com.hd123.rumba.gwt.entry.server.menu.QMenuReader;
import com.hd123.rumba.navigation.menu.BMenu;
import com.hd123.rumba.navigation.menu.BMenuItem;
import com.hd123.rumba.navigation.menu.IBMenuMgr;
import com.hd123.rumba.runtime.NamingMgr;

/**
 * 应用程序主界面框架 | 远程服务 | 实现类
 * 
 * @author lxm
 * 
 */
public class MainFrameServiceImpl extends RBRemoteServiceServlet implements MainFrameService {

  public QMenu getSubMenu(String parentItemId) throws ClientBizException {
    try {
      BMenu mainMenu = this.getCurrentMainMenu();
      BMenuItem parentItem = mainMenu.getItemById(parentItemId); 
      if (parentItem == null)
        return new QMenu();
      
      BMenu subMenu = this.getMenuMgr().cloneSubtree(mainMenu, parentItem);
      
      QMenuReader reader = new QMenuReader();
      reader.setTarget(new QMenu());
      reader.read(subMenu);
      return reader.getTarget();
      
    } catch (HibernateException e) {
      this.reportError("无法取得主菜单信息。", e);
      throw this.buildException("无法取得主菜单信息。", e);
    } catch (BusinessException e) {
      this.reportError("无法取得主菜单信息。", e);
      throw this.buildException("无法取得主菜单信息。", e);
    }
  }

  public List getTopMenuItems() throws ClientBizException {
    try {
      BMenu mainMenu = this.getCurrentMainMenu();
      List<BMenuItem> blist = mainMenu.getDirectChildren(null);
      List retList = new ArrayList();
      QMenuItemReader reader = new QMenuItemReader();
      for (BMenuItem bitem : blist) {
        reader.setTarget(new QMenuItem());
        reader.read(bitem);
        retList.add(reader.getTarget());
      }
      return retList;
    } catch (BusinessException e) {
      this.reportError("无法取得主菜单信息。", e);
      throw this.buildException("无法取得主菜单信息。", e);
    }
  }

  private IDomainMgr domainMgr = null;
  private IBMenuMgr menuMgr = null;
  private IBCartridgeMgr cartMgr = null;

  private IDomainMgr getDomainMgr() throws BusinessException {
    if (domainMgr == null)
      domainMgr = (IDomainMgr) NamingMgr.getInstance().lookupMgr(IDomainMgr.class);
    return domainMgr;
  }

  private IBMenuMgr getMenuMgr() throws BusinessException {
    if (menuMgr == null)
      menuMgr = (IBMenuMgr) NamingMgr.getInstance().lookupMgr(IBMenuMgr.class);
    return menuMgr;
  }

  private IBCartridgeMgr getCartMgr() throws BusinessException {
    if (cartMgr == null)
      cartMgr = (IBCartridgeMgr) NamingMgr.getInstance().lookupMgr(IBCartridgeMgr.class);
    return cartMgr;
  }

  /**
   * 取得当前用户的主菜单。
   * 
   * @return
   * @throws BusinessException
   */
  private BMenu getCurrentMainMenu() throws BusinessException, ClientBizException {
    try {
      List<ICartFunctionView> cfvs = getDomainMgr().getCFVsByDomain(this.getCurrentDomainPerz());
      BMenu mainMenu = getMenuMgr().createMenu();
      for (int i = 0; i < cfvs.size(); i++) {
        BCartFunctionView cfv = getCartMgr().getFunctionView(cfvs.get(i).getUuid());
        mainMenu.merge(cfv.getMainMenu());
      }
      mainMenu.obscurByUser(this.getCurrentUserPerz());
      return mainMenu;
    } catch (HibernateException e) {
      throw new BusinessException(e);
    }
  }

}
